#!/bin/bash

# Flowable 服务启动脚本

echo "=========================================="
echo "Flowable 工作流引擎启动脚本"
echo "=========================================="
echo ""

# 检查 Docker 是否运行
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker 未运行，请先启动 Docker"
    exit 1
fi

echo "✅ Docker 运行正常"

# 检查 docker-compose 是否可用
if ! command -v docker-compose &> /dev/null; then
    echo "❌ docker-compose 未安装，请先安装 docker-compose"
    exit 1
fi

echo "✅ docker-compose 可用"

# 创建必要的目录
echo ""
echo "创建必要的目录..."
mkdir -p flowable/logs
mkdir -p flowable/processes
echo "✅ 目录创建完成"

# 启动服务
echo ""
echo "启动 Flowable 服务..."
docker-compose up -d

# 等待服务启动
echo ""
echo "等待服务启动（最多 60 秒）..."
timeout=60
elapsed=0
while [ $elapsed -lt $timeout ]; do
    if curl -s -u admin:test http://localhost:8081/flowable-rest/service/management/engine > /dev/null 2>&1; then
        echo "✅ Flowable REST API 已就绪"
        break
    fi
    sleep 2
    elapsed=$((elapsed + 2))
    echo -n "."
done

echo ""
echo "=========================================="
echo "服务启动完成"
echo "=========================================="
echo ""
echo "Flowable UI: http://localhost:8080/flowable-ui"
echo "默认账号: admin / test"
echo ""
echo "Flowable REST API: http://localhost:8081/flowable-rest"
echo ""
echo "查看服务状态: docker-compose ps"
echo "查看日志: docker-compose logs -f"
echo "停止服务: docker-compose down"
echo ""

